package com.huadao.pda.material;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.huadao.pda.R;
import com.huadao.pda.base.BaseDialogFragment;
import com.huadao.pda.base.BasePresenter;
import com.huadao.pda.common.TimeFormat;
import com.huadao.pda.data.bean.Dispensing;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MaterialDetailDF extends BaseDialogFragment {

    @BindView(R.id.tv_materialNumber)
    TextView tvMaterialNumber;
    @BindView(R.id.tv_materialName)
    TextView tvMaterialName;
    @BindView(R.id.tv_batchNumber)
    TextView tvBatchNumber;
    @BindView(R.id.tv_specifications)
    TextView tvSpecifications;
    @BindView(R.id.tv_expiryDate)
    TextView tvExpiryDate;
    @BindView(R.id.tv_note)
    TextView tvNote;
    @BindView(R.id.btn_cancel)
    TextView btn_cancel;
    @BindView(R.id.btn_confirm_get)
    TextView btn_confirm_get;

    private Dispensing dispensing;
    private OnGetMaterial onGetMaterial;
    private boolean isGetable;

    interface OnGetMaterial{
        void onGetMaterial(Dispensing dispensing);
    }

    public static MaterialDetailDF newInstance(Dispensing dispensing,boolean isGetable) {

        Bundle args = new Bundle();
        args.putSerializable("dispensing",dispensing);
        args.putBoolean("isGetable",isGetable);
        MaterialDetailDF fragment = new MaterialDetailDF();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialogfragment_material;
    }

    @Override
    protected double dialogWidthScale() {
        return 0.8;
    }

    @Override
    protected double dialogHeightScale() {
        return 0.7;
    }

    @Override
    protected void onInit() {
        Bundle bundle=getArguments();
        if(bundle!=null){
            dispensing= (Dispensing) bundle.get("dispensing");
            isGetable=bundle.getBoolean("isGetable");
        }
        if(dispensing!=null){
            initData();
        }
        if(isGetable&&!dispensing.isUse()){
            btn_cancel.setVisibility(View.VISIBLE);
            btn_confirm_get.setVisibility(View.VISIBLE);
        }else {
            btn_cancel.setVisibility(View.VISIBLE);
            btn_confirm_get.setVisibility(View.GONE);
        }
    }

    private void initData() {
        tvMaterialNumber.setText(dispensing.getBarCode());
        tvMaterialName.setText(dispensing.getName());
        tvBatchNumber.setText(dispensing.getBatchCode());
        tvSpecifications.setText(dispensing.getSpecification());
        if (dispensing.getExpirationDate() != null) {
            tvExpiryDate.setText(TimeFormat.date_sdf.format(dispensing.getExpirationDate()));
        }
        tvNote.setText(dispensing.getRemark());
    }

    @OnClick({R.id.btn_cancel, R.id.btn_confirm_get})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                this.dismiss();
                break;
            case R.id.btn_confirm_get:
                if(onGetMaterial!=null){
                    onGetMaterial.onGetMaterial(dispensing);
                }
                this.dismiss();
                break;
        }
    }

    public void setOnGetMaterial(OnGetMaterial onGetMaterial) {
        this.onGetMaterial = onGetMaterial;
    }
}
